//
//  KABCosPubEditVC.m
//  kabu
//
//  Created by XiangDongMBP on 2017/2/8.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABCosPubEditVC.h"
#import "KABCosPubEditTCell.h"
#import "KABModel.h"

@interface KABCosPubEditVC ()<UITableViewDelegate, UITableViewDataSource, KABCosPubEditTCellDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

static NSString *const kReuseCellId = @"KABCosPubEditTCell";

@implementation KABCosPubEditVC

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNavigationBar];
    [self initSubViews];
}

- (void)initNavigationBar {
    self.navigationItem.title = @"编辑";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(completeBarBtnTapped:)];
    self.navigationItem.hidesBackButton = YES;
}

- (void)initSubViews {
    UINib *cellNib = [UINib nibWithNibName:kReuseCellId bundle:nil];
    [self.tableView registerNib:cellNib forCellReuseIdentifier:kReuseCellId];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 44.0;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.pubModel.cosplay_detail.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KABCosPubEditTCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuseCellId forIndexPath:indexPath];
//    [cell configViewWithPublishItem:self.pubModel.cosplay_detail[indexPath.section]];
    cell.item = self.pubModel.cosplay_detail[indexPath.section];
    cell.delegate = self;
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5.9;
}

#pragma mark - KABCosPubEditTCellDelegate
- (void)kABCosPubEditTCell:(KABCosPubEditTCell *)cell deleteBtnTapped:(UIButton *)btn {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [self.pubModel removePublishItemAtIndex:indexPath.section];
    [self.tableView beginUpdates];
    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
}

- (void)kABCosPubEditTCell:(KABCosPubEditTCell *)cell textViewDidChanged:(UITextView *)textView {
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

#pragma mark - event response
- (void)completeBarBtnTapped:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - private methods
#pragma mark - network request
#pragma mark - getters and setters

@end
